package com.market.domain;

import java.util.Locale;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "productprice")
public class ProductPrice extends BaseObject {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1973579343446635683L;

	@Override
	public boolean equalsId(Object o) {
		if ((o instanceof ProductPrice)
				&& ((ProductPrice) o).getId().equals(this.id)) {
			return true;
		}
		return false;
	}

	public ProductPrice() {
		super();
	}

	public ProductPrice(Product product) {
		super();
		super.setIsNew(true);
		this.product = product;
		this.price = product.getPrice();
		this.specialPrice = product.getSpecialPrice();
		this.unitPrice = product.getUnitPrice();
		this.chineseUnitPrice = product.getChineseUnitPrice();
		this.frenchUnitPrice = product.getFrenchUnitPrice();
	}

	@Transient
	public String getUnitPriceLabel() {
		return super.getLabel("UnitPrice", super.getLocale());
	}

	@Transient
	public Boolean getIsOnSpecial() {
		return !this.specialPrice.equals(this.price);
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id = 0L;
	@Column(columnDefinition = "Double Default 0")
	private Double price = 0D;
	@Column(columnDefinition = "Double Default 0")
	private Double specialPrice = 0D;
	@Column(length = 50)
	private String unitPrice; // $0.99/lb
	@Column(length = 50)
	private String chineseUnitPrice;
	@Column(length = 50)
	private String frenchUnitPrice;
	@ManyToOne(fetch = FetchType.EAGER)
	private Product product;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public Double getSpecialPrice() {
		return specialPrice;
	}

	public void setSpecialPrice(Double specialPrice) {
		this.specialPrice = specialPrice;
	}

	public String getUnitPrice() {
		if (this.unitPrice == null || this.unitPrice.isEmpty()) {
			return null;
		}
		return unitPrice;
	}

	public void setUnitPrice(String unitPrice) {
		this.unitPrice = unitPrice;
	}

	public String getChineseUnitPrice() {
		return chineseUnitPrice;
	}

	public void setChineseUnitPrice(String chineseUnitPrice) {
		this.chineseUnitPrice = chineseUnitPrice;
	}

	public String getFrenchUnitPrice() {
		return frenchUnitPrice;
	}

	public void setFrenchUnitPrice(String frenchUnitPrice) {
		this.frenchUnitPrice = frenchUnitPrice;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime
				* result
				+ ((chineseUnitPrice == null) ? 0 : chineseUnitPrice.hashCode());
		result = prime * result
				+ ((frenchUnitPrice == null) ? 0 : frenchUnitPrice.hashCode());
		result = prime * result + ((price == null) ? 0 : price.hashCode());
		result = prime * result
				+ ((specialPrice == null) ? 0 : specialPrice.hashCode());
		result = prime * result
				+ ((unitPrice == null) ? 0 : unitPrice.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ProductPrice other = (ProductPrice) obj;
		if (chineseUnitPrice == null) {
			if (other.chineseUnitPrice != null)
				return false;
		} else if (!chineseUnitPrice.equals(other.chineseUnitPrice))
			return false;
		if (frenchUnitPrice == null) {
			if (other.frenchUnitPrice != null)
				return false;
		} else if (!frenchUnitPrice.equals(other.frenchUnitPrice))
			return false;
		if (price == null) {
			if (other.price != null)
				return false;
		} else if (!price.equals(other.price))
			return false;
		if (specialPrice == null) {
			if (other.specialPrice != null)
				return false;
		} else if (!specialPrice.equals(other.specialPrice))
			return false;
		if (unitPrice == null) {
			if (other.unitPrice != null)
				return false;
		} else if (!unitPrice.equals(other.unitPrice))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "ProductPrice [id=" + id + ", price=" + price
				+ ", specialPrice=" + specialPrice + ", unitPrice=" + unitPrice
				+ ", chineseUnitPrice=" + chineseUnitPrice
				+ ", frenchUnitPrice=" + frenchUnitPrice + "]";
	}

}
